package com.qy.action;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.qy.model.PageModel;
import com.qy.model.Photo;
import com.qy.service.UserService;

@Controller("savePhotosAction")
@ParentPackage("json-default")
public class PhotosAction {
	private File file;
	private String fileFileName;
	private String fileFileContentType;
	private Photo photo;
	@Autowired
	private UserService userService;
	private Map<String, Object> map = new HashMap<>();
	private List<Photo> list = new ArrayList<>();
	@Autowired
	private PageModel pageModel;
	
	public List<Photo> getList() {
		return list;
	}

	public void setList(List<Photo> list) {
		this.list = list;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public Map<String, Object> getMap() {
		return map;
	}

	public void setMap(Map<String, Object> map) {
		this.map = map;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getFileFileContentType() {
		return fileFileContentType;
	}

	public void setFileFileContentType(String fileFileContentType) {
		this.fileFileContentType = fileFileContentType;
	}

	public Photo getPhoto() {
		return photo;
	}

	public void setPhoto(Photo photo) {
		this.photo = photo;
	}

	@Action(value = "savephotos", results = { @Result(type = "json", params = { "root", "map" }) })
	public String savephoto() throws Exception {
		System.out.println(fileFileName);
		System.out.println(file);
		String url = userService.saveFile(fileFileName, file);
		photo.setUrl(url);
		try {
			userService.save(photo);
			map.put("result", true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put("result", false);
		}
		return "success";
	}
	@Action(value = "selectSowing", results = { @Result(type = "json", params = { "root", "map" }) })
	public String showSowing() {
		
		String hql = "from com.qy.model.Photo";
		try {
			List<Photo> list1 = new ArrayList<>();
			list1 = userService.select(hql, null);
			pageModel.setTotalSize(list1.size());
			pageModel.setCurrentPage(1);
			System.out.println(pageModel.getPageSize());
			list = userService.selectAllByPage(hql, null, pageModel);
			map.put("page", pageModel);
			map.put("photo", list);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "success";
	}
	@Action(value = "deletesowing", results = { @Result(type = "json", params = { "root", "map" }) })
	public String deletesowing() {
		try {
			userService.delete(photo);
			map.put("result", true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put("result", false);
		}
		return "success";
	}
	@Action(value = "selectsowing", results = { @Result(type = "json", params = { "root", "photo" }) })
	public String selectsowing() {
		String hql = "from com.qy.model.Photo photo where photo.id = ?";
		Object[] object =new Object[1];
		object[0]=photo.getId();
		try {
			photo = (Photo) userService.select(hql, object).get(0);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "success";
	}
	@Action(value = "saveUpdate", results = { @Result(type = "json", params = { "root", "map" }) })
	public String saveUpdate() throws Exception {
		String url;
		String hql = "update com.qy.model.Photo photo set photo.name=?,photo.url=?,photo.position=?"
				+ "where photo.id=?";
		if (fileFileName==null) {
			url=photo.getUrl();
		}else{
			url= userService.saveFile(fileFileName, file);
		}
		List<Object> list = new ArrayList<>();
		list.add(photo.getName());
		list.add(url);
		list.add(photo.getPosition());
		list.add(photo.getId());
		try {
			userService.execute(hql, list);
			map.put("isSuccess", true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put("isSuccess", false);
		}
		return "success";
	}
}
